#include<time.h>
#include "ringQueue.hpp"

void* Produce(void* args)
{
    ringQueue<int>* rq = (ringQueue<int>*)args;
    while(true)
    {
        int data = rand()%100;
        rq->Push(data);
        cout << pthread_self() <<" 生产者：" << data << endl;
        sleep(2);
    }
}

void* Consume(void* args)
{
    ringQueue<int>* rq = (ringQueue<int>*)args;
    while(true)
    {
        int data = 0;
        rq->Pop(&data);

        cout << pthread_self() << " 消费者：" << data << endl;
        sleep(1);
    }
}

int main()
{

    srand((unsigned int)time(nullptr));
    ringQueue<int>* rq = new ringQueue<int>();

    pthread_t cmer[2],ptor[2];
    pthread_create(ptor,nullptr,Produce,(void*)rq);
    pthread_create(ptor+1,nullptr,Produce,(void*)rq);
    pthread_create(cmer,nullptr,Consume,(void*)rq);
    pthread_create(cmer+1,nullptr,Consume,(void*)rq);

    pthread_join(ptor[0],nullptr);
    pthread_join(ptor[1],nullptr);
    pthread_join(cmer[0],nullptr);
    pthread_join(cmer[1],nullptr);

    delete rq;

    return 0;
}


